package com.qf.commons.standar.vaild;


import com.qf.commons.core.utils.ApplicationUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * description: 自定义的校验处理器
 * author: Ken
 * 公众号：Java架构栈
 */
public class QfConstraint implements ConstraintValidator<QfVaild, Object> {

    private QfVaild qfVaild;

    @Override
    public void initialize(QfVaild qfVaild) {
        this.qfVaild = qfVaild;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        //如果是空值，则直接放行
        if(value == null) return true;
        //获取开发者自定义的处理器实现类型
        Class<? extends IQfValid> handler = qfVaild.handler();
        IQfValid qfValid = ApplicationUtils.getBean(handler);
        if (qfValid == null) return true;
        //交给开发者的实现类去校验
        return qfValid.vaild(qfVaild, value);
    }
}
